using ColorFillGame.Domain.Exceptions;

namespace ColorFillGame.Domain.ValueObjects;

public class PlayerColor : ValueObject
{
    public string Name { get; private set; }
    public string HexCode { get; private set; }
    public string Code => HexCode; // Alias for HexCode used in DTOs

    private static readonly Dictionary<string, string> _colorMap = new()
    {
        { "Red", "#FF4136" },
        { "Blue", "#0074D9" },
        { "Green", "#2ECC40" },
        { "Yellow", "#FFDC00" },
        { "Purple", "#B10DC9" },
        { "Orange", "#FF851B" },
        { "Teal", "#39CCCC" },
        { "Pink", "#FF1493" }
    };

    private PlayerColor()
    {
        // EF Core constructor
    }

    public PlayerColor(string colorName)
    {
        if (string.IsNullOrWhiteSpace(colorName))
            throw new DomainException("Color name cannot be empty.");
        
        if (!_colorMap.ContainsKey(colorName))
            throw new DomainException($"Invalid color name: {colorName}");
        
        Name = colorName;
        HexCode = _colorMap[colorName];
    }

    public static string[] GetAvailableColors()
    {
        return _colorMap.Keys.ToArray();
    }

    public static bool IsValidColor(string colorName)
    {
        return _colorMap.ContainsKey(colorName);
    }

    public static PlayerColor FromName(string colorName)
    {
        return new PlayerColor(colorName);
    }

    protected override IEnumerable<object> GetAtomicValues()
    {
        yield return Name;
        yield return HexCode;
    }
}